<template>
  <el-card class="common-container">
    <div style="margin-bottom: 20px; font-size: 20px;color: #606266;">行业列表</div>
    <div class="search-container">
      <el-button class="addBtn" type="primary" icon="add" @click="handleAdd">新增行业</el-button>
    </div>
    <el-form ref="form" :model="query" :inline="true" size="small">
        <el-form-item>
          <el-input v-model="query.name" placeholder="请输入行业名称" />
        </el-form-item>
        <el-form-item>
          <el-select v-model="query.status" placeholder="请选择状态">
            <el-option label="全部" value="" />
            <el-option label="未启用" value="1" />
            <el-option label="已启用" value="0" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getList('init')">搜索</el-button>
          <el-button type="primary" @click="empty">重置</el-button>
        </el-form-item>
      </el-form>
    <el-table :data="list" border>
      <el-table-column prop="name" label="行业名称" />
      <el-table-column prop="description" label="行业说明" show-overflow-tooltip/>
      <el-table-column prop="useCount" label="使用量" />
      <el-table-column prop="status" label="状态">
         <template slot-scope="scope">{{ scope.row.status == '0'? '已启用':'未启用' }}</template>
         </el-table-column>
      <el-table-column prop="createTime" label="创建时间" />
      <el-table-column align="center" label="操作" width="250px">
        <template slot-scope="scope">
          <el-button size="small" type="primary" @click="handleEdit(scope.row.id)">编辑</el-button>
          <el-button size="small" type="danger" @click="handleDelete(scope.row.id)" :disabled="scope.row.buer">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      style="margin-top: 10px "
      background
      :current-page.sync="pageNum"
      :page-size.sync="pageSize"
      layout="total, prev, pager, next"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </el-card>
</template>

<script>
import { listBusiness,delBusiness } from '@/api/business'

export default {
  name: 'BusinessList',
  data() {
    return {
      // title: null, // 搜索内容
      pageNum: 1,
      pageSize: 10,
      list: [],
      total: 0,
      query: {
        name: '', // 行业名称
        status: '' // 选择状态
      },
    }
  },
  mounted() {
    this.getList()
  },

  methods: {
    // 重置
    empty() {
      this.query = {
        name: '', // 行业名称
        status: '' // 选择状态
      }
      this.getList('init')
    },

    getList(type) {
      if (type == 'init') {
        this.pageNum = 1
      }
      const obj = {
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        ...this.query
      }
      listBusiness(obj).then((res) => {
        res.data.list.forEach((i,index) => {
            i.buer = i.useCount>0 && i.status =='0'
          });
        this.list = res.data.list
        this.total = res.data.total
        return res
      })
    },
    handleAdd() {
      this.$router.push({ name: 'addBusiness' })
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getList()
    },

    handleCurrentChange() {
      this.getList()
    },

    handleEdit(id) {
      this.$router.push({ name: 'editBusiness', params: { id: id } })
    },
    handleDelete(id) {
      this.$confirm('是否删除当前行业，删除后不可恢复，请谨慎操作！', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delBusiness({bizPackageId:id}).then((res) => {
          if (res.code === 0) {
            this.getList('init')
            this.$message.success('删除成功')
          } else {
            this.$message.error(res.msg)
          }
        })
      })
    },
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .search-container {
    margin-top: 20px;
    margin-bottom: 20px
  }
  .search-wrap {
    width: 300px;
    margin-right: 10px
  }
  .addBtn{
	  background-color: $btnBgc;
	  border-color: $btnBgc;
  }
  .addBtn:hover{
	  opacity: 0.5;
  }
  .editBtn{
	  background-color: $warning;
	  border-color: $warning;
  }
  ::v-deep .el-pagination.is-background .el-pager li:not(.disabled).active{
	  background-color: $btnBgc;
  }
  ::v-deep .el-pagination.is-background .el-pager li:not(.disabled):hover{
		color: white;
		background-color: $btnBgc;
	}
</style>
